﻿using System.Collections.ObjectModel;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Unity;
using StockholmsStad.Runtime.Serialization.DataContract;
using StockholmsStad.Windows.UI.Infrastructure.Interfaces;
using StockholmsStad.Windows.UI.Modules.Maintenance.Interfaces;
using StockholmsStad.Windows.UI.Facade;
using StockholmsStad.Windows.UI.Modules.Maintenance.Models;

namespace StockholmsStad.Windows.UI.Modules.Maintenance.Services
{
    public class InformationTextService : IInformationTextService
    {

        private IApplicationService applicationService;
        [Dependency]
        public IApplicationService ApplicationService
        {
            set { applicationService = value; }
        }

        public InformationTextPresentationModel GetDefaultPresentationModel()
        {
            var model = new InformationTextPresentationModel();

            var user = applicationService.GetCurrentUser();
            var facade = new MaintenanceFacade(user);
            
            model.TextTypes = facade.GetTextTypes();

            if(model.TextTypes.Count > 0)
            {
                var texts = new ObservableCollection<InformationText>();

                var types = facade.GetTexts(model.TextTypes[0].Id);

                foreach (InformationText info in types)
                {
                    texts.Add(info);
                }

                model.Texts = texts;
            }
           
            return model;
        }

        public ObservableCollection<InformationText> GetInformationTextsFiltered(int typeId)
        {
            var user = applicationService.GetCurrentUser();
            var facade = new MaintenanceFacade(user);
            var list = new ObservableCollection<InformationText>(facade.GetTexts(typeId));
            return list;
        }

        public int AddInformationText(InformationText item)
        {
            var user = applicationService.GetCurrentUser();
            return new MaintenanceFacade(user).AddText(item);
        }

        public void UpdateInformationText(InformationText item)
        {
            var user = applicationService.GetCurrentUser();
            new MaintenanceFacade(user).UpdateText(item);
        }

        public void DeleteInformationText(InformationText item)
        {
            var user = applicationService.GetCurrentUser();
            new MaintenanceFacade(user).DeleteText(item);
        }
    }
}